package com.weixx.yctp.cp.service;

import cc.eamon.open.status.StatusException;
import com.weixx.yctp.cp.dao.PcDao;
import com.weixx.yctp.cp.entity.Pc;
import com.weixx.yctp.cp.entity.PcDetailMapper;
import com.weixx.yctp.cp.entity.PcExample;
import com.weixx.yctp.cp.entity.PcPostMapper;
import com.weixx.yctp.cp.entity.PcSimpleMapper;
import com.weixx.yctp.cp.entity.PcUpdateMapper;
import java.lang.Byte;
import java.lang.Integer;
import java.lang.Long;
import java.lang.Object;
import java.lang.String;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author: eamon
 * Email: eamon@eamon.cc */
@Service
public class PcService {
  @Autowired
  private PcDao pcDao;

  /**
   * 分页计数函数 */
  private PcExample initPageRowQueryFilter(Long page, Integer rows) {
    PcExample example = new PcExample();
    example.setOrderByClause("updateTime desc");
    PcExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0);
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    return example;
  }

  /**
   * 参数过滤函数 */
  private PcExample initQueryFilter(Long page, Integer rows, Integer id, String pcTime,
      String pcState, Date pcStartTime, Date pcEndTime, Date createTime, String createBy,
      Date updateTime, String updateBy, Byte isDelete, String expand1field, String expand2field) {
    PcExample example = new PcExample();
    example.setOrderByClause("updateTime desc");
    PcExample.Criteria criteria = example.createCriteria();
    if(id!=null) {
      criteria.andIdEqualTo(id);
    }
    if(pcTime!=null && !pcTime.equals("")) {
      criteria.andPcTimeEqualTo(pcTime);
    }
    if(pcState!=null && !pcState.equals("")) {
      criteria.andPcStateEqualTo(pcState);
    }
    if(pcStartTime!=null) {
      criteria.andPcStartTimeEqualTo(pcStartTime);
    }
    if(pcEndTime!=null) {
      criteria.andPcEndTimeEqualTo(pcEndTime);
    }
    if(createTime!=null) {
      criteria.andCreateTimeEqualTo(createTime);
    }
    if(createBy!=null && !createBy.equals("")) {
      criteria.andCreateByEqualTo(createBy);
    }
    if(updateTime!=null) {
      criteria.andUpdateTimeEqualTo(updateTime);
    }
    if(updateBy!=null && !updateBy.equals("")) {
      criteria.andUpdateByEqualTo(updateBy);
    }
    if(isDelete!=null) {
      criteria.andIsDeleteEqualTo(isDelete);
    }
    if(expand1field!=null && !expand1field.equals("")) {
      criteria.andExpand1fieldEqualTo(expand1field);
    }
    if(expand2field!=null && !expand2field.equals("")) {
      criteria.andExpand2fieldEqualTo(expand2field);
    }
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    return example;
  }

  public Long getPcCount() throws StatusException {
    return pcDao.countByExample(initPageRowQueryFilter(0L, 0));
  }

  public Long getPcFilterCount(Integer id, String pcTime, String pcState, Date pcStartTime,
      Date pcEndTime, Date createTime, String createBy, Date updateTime, String updateBy,
      Byte isDelete, String expand1field, String expand2field) throws StatusException {
    return pcDao.countByExample(initQueryFilter(0L, 0, id, pcTime, pcState, pcStartTime, pcEndTime, createTime, createBy, updateTime, updateBy, isDelete, expand1field, expand2field));
  }

  public Pc getPcByPrimaryKey(Integer key) throws StatusException {
    return pcDao.selectByPrimaryKey(key);
  }

  public Map<String, Object> getPcSimpleMapByPrimaryKey(Integer key) throws StatusException {
    Pc entity = pcDao.selectByPrimaryKey(key);
    if(entity == null) throw new StatusException("ENTITY_NULL");
    return PcSimpleMapper.getMap(entity);
  }

  public Map<String, Object> getPcDetailMapByPrimaryKey(Integer key) throws StatusException {
    Pc entity = pcDao.selectByPrimaryKey(key);
    if(entity == null) throw new StatusException("ENTITY_NULL");
    return PcDetailMapper.getMap(entity);
  }

  public List<Map<String, Object>> getPcSimpleMapList(Long page, Integer rows) throws
      StatusException {
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Pc entity: pcDao.selectByExample(initPageRowQueryFilter(page, rows))) {
      entityMapList.add(PcSimpleMapper.getMap(entity));
    }
    return entityMapList;
  }

  public List<Map<String, Object>> getPcDetailMapList(Long page, Integer rows) throws
      StatusException {
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Pc entity: pcDao.selectByExample(initPageRowQueryFilter(page, rows))) {
      entityMapList.add(PcDetailMapper.getMap(entity));
    }
    return entityMapList;
  }

  public Map<String, Object> postPc(PcPostMapper postMapper, String createBy, String updateBy)
      throws StatusException {
    // 记录上传条目数;
    AtomicInteger count = new AtomicInteger();
    // 从上传数据中拿出记录;
    Pc entity = postMapper.getEntity();
    // 记录创建者;
    entity.setCreateBy(createBy);
    // 记录更新者;
    entity.setUpdateBy(updateBy);
    // 持久化对象;
    try {
      count.addAndGet(pcDao.insertSelective(entity));
    }
    catch (Exception e) {
      throw new StatusException("ENTITY_EXIST");
    }
    return PcDetailMapper.getMap(entity);
  }

  public List<Map<String, Object>> postPcList(List<PcPostMapper> postMappers, String createBy,
      String updateBy) throws StatusException {
    // 记录上传条目数;
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    // 将List中所有的Entity均上传;
    for (PcPostMapper postMapper: postMappers) {
      // 插入计数;
      entityMapList.add(postPc(postMapper, createBy, updateBy));
    }
    return entityMapList;
  }

  public Map<String, Object> updatePc(PcUpdateMapper updateMapper, String updateBy) throws
      StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 从更新数据中拿出记录;
    Pc entity = updateMapper.getEntity();
    // 记录更新者;
    entity.setUpdateBy(updateBy);
    try {
      count.addAndGet(pcDao.updateByPrimaryKeySelective(entity));
    }
    catch (Exception e) {
      throw new StatusException("ENTITY_NULL");
    }
    return PcDetailMapper.getMap(entity);
  }

  public List<Map<String, Object>> updatePcList(List<PcUpdateMapper> updateMappers, String updateBy)
      throws StatusException {
    // 记录更新条目数;
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    // 将List中所有的Entity更新;
    for (PcUpdateMapper updateMapper: updateMappers) {
      // 更新计数;
      entityMapList.add(updatePc(updateMapper, updateBy));
    }
    return entityMapList;
  }

  public Integer deletePc(Integer key, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 从更新数据中拿出记录;
    Pc entity = new Pc();
    // 记录更新者;
    entity.setId(key);
    entity.setUpdateBy(updateBy);
    // 删除条目;
    entity.setIsDelete((byte) 1);
    count.addAndGet(pcDao.updateByPrimaryKeySelective(entity));
    return count.get();
  }

  public Integer deletePcList(List<Integer> keys, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 将List中所有的Entity更新;
    for (Integer key: keys) {
      // 更新计数;
      count.addAndGet(deletePc(key, updateBy));
    }
    return count.get();
  }

  public Integer recoverPc(Integer key, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 从更新数据中拿出记录;
    Pc entity = new Pc();
    // 记录更新者;
    entity.setId(key);
    entity.setUpdateBy(updateBy);
    // 恢复条目;
    entity.setIsDelete((byte) 0);
    count.addAndGet(pcDao.updateByPrimaryKeySelective(entity));
    return count.get();
  }

  public Integer recoverPcList(List<Integer> keys, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 将List中所有的Entity更新;
    for (Integer key: keys) {
      // 更新计数;
      count.addAndGet(recoverPc(key, updateBy));
    }
    return count.get();
  }

  public List<Map<String, Object>> getPcSimpleMapByTime(Long page, Integer rows, Date startTime, Date endTime)
          throws StatusException {
    PcExample example = new PcExample();
    example.setOrderByClause("updateTime desc");
    PcExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0).andCreateTimeBetween(startTime,endTime);
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Pc entity: pcDao.selectByExample(example)) {
      entityMapList.add(PcDetailMapper.getMap(entity));
    }
    return entityMapList;
  }


  //获取当前进行的批次
  public List<Map<String, Object>> getOnDongPc()
          throws StatusException {
    PcExample example = new PcExample();
    PcExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0).andPcStateEqualTo("0");//0在进行，1未开始，2已结束
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Pc entity: pcDao.selectByExample(example)) {
      Map<String, Object> inner=new HashMap<>();
      inner.put("id",entity.getId());
      inner.put("pcTime",entity.getPcTime());
      entityMapList.add(inner);
    }
    return entityMapList;
  }

}
